<?php
/**
 * 砍价活动model
 * User:
 * Date: 2017/7/12
 * Time: 16:37
 */

defined('InNcPHP') or exit('Access Invalid!');

 class bargainModel extends  Model{


   const BARGAIN_STATE_NORMAL = 1;
   const BARGAIN_STATE_REVIEW = 0;

   public function __construct()
   {
       parent::__construct('bargain');
   }

     /**
      * 活动分页列表
      * @param array $condition
      * @param string $field
      * @param int $page
      * @param string $order
      * @return mixed
      */
     public function getBarginlistPage($condition = array(), $field = '*', $page = null, $order = 'bargain_id desc '){

         return $this->field($field)->where($condition)->page($page)->order($order)->select();

     }

     /**
      * 添加
      * @param array $param
      * @return mixed
      */
     public function addBargin($param){

         return $this->insert($param);
     }

     /**
      * 编辑
      * @param array $condition
      *  @param array $param
      */
     public function updateBargin($condition, $param){

         return $this->where($condition)->update($param);


     }

     /**
      * 获取一条记录的详细信息
      * @param array $condition
      * @param string $field
      * @return mixed
      */
     public function getBargainInfo($condition = array(), $field = '*'){

         return $this->field($field)->where($condition)->find();

     }
     /**
      * 删除活动记录
      * @param array $condition
      */
     public function delBargain($condition){

         return $this->where($condition)->delete();
     }

     /**
      * 获取某条活动是否正常开启
      * @param $bargain_id
     */

     public  function  checkActivity($bargain_id){

         $condition['if_show'] = self::BARGAIN_STATE_NORMAL;
         $condition['start_time'] = array('lt',TIMESTAMP);
         $condition['end_time'] = array('gt',TIMESTAMP);
         $condition['if_show'] = array('neq',0);
         $condition['bargain_id'] = $bargain_id;
         return $this->where($condition)->find();

     }

     /**
      * 增加活动人数
      * @param $bargain_id
      */
     public function addbargainres($bargain_id ){
         $this->where(array('bargain_id'=>$bargain_id))->setInc('bg_rs');

     }

     /**
      * 增加分享次数
      * @param $bargain_id
      */
     public function add_share_sun($bargain_id){

         $this->where(array('bargain_id'=>$bargain_id))->setInc('share_sun');
     }

     /**
      * 增加活动参与成功人数
      * @param $bargain_id
      */
     public function true_member($bargain_id){

         $this->where(array('bargain_id'=>$bargain_id))->setInc('bg_true_rs');

     }

     /**
      * 减少活动库存
      * @param $bargain_id
      */
     public  function reduce_store($bargain_id){
         $this->where(array('bargain_id'=>$bargain_id))->setDec('bg_store');
     }


}